package com.baibei.product;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.model.ProductInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.rae.widget.RaePlaceHolderLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 商品详情
 * Created by ChenRui on 2017/8/9 0009 11:50.
 */
@Route(path = AppRouter.ROUTER_PRODUCT_DETAIL)
public class ProductDetailActivity extends BasicActivity {

    @BindView(R2.id.placeholder)
    RaePlaceHolderLayout mPlaceHolderLayout;
    @BindView(R2.id.layout_webview)
    FrameLayout mLayoutWebview;
    private WebView mWebView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        ButterKnife.bind(this);
        ProductInfo productInfo = getIntent().getParcelableExtra("productInfo");

        mWebView = new WebView(getApplicationContext());
        mWebView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mLayoutWebview.addView(mWebView);
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDisplayZoomControls(false);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowContentAccess(true);
        settings.setAllowFileAccess(true);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                mPlaceHolderLayout.dismiss();
                mPlaceHolderLayout.onEmpty();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mPlaceHolderLayout.dismiss();
            }
        });
        mWebView.loadUrl(productInfo.getProductDetails());
    }

    @Override
    protected void onDestroy() {
        mLayoutWebview.removeAllViews();
        super.onDestroy();
        if (mWebView != null) {
            mWebView.removeAllViews();
            mWebView.destroy();
        }
    }

    @Override
    public IPresenter getBasicPresenter() {
        return null;
    }
}
